//
//  WMSetViewController.m
//  witMorning
//
//  Created by Mac mini on 2018/1/2.
//  Copyright © 2018年 zt. All rights reserved.
//

#import "WMSetViewController.h"
#import "SetHeaderCell.h"
#import "SettingCell.h"
#import "WMChangePsdView.h"
#import "LoginViewController.h"
#import "WMCAlertView.h"

@interface WMSetViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)WMChangePsdView *changePsdView;
@property (nonatomic,strong)UITableView *setTabelView;
@property (nonatomic,strong)UIView *maskView;

@end

@implementation WMSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"设置";
    self.view.backgroundColor = [UIColor darkGrayColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    
    [self createTable];
}

-(void)createTable{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 10, SCREEN_W, SCREEN_H) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [tableView registerClass:[SetHeaderCell class] forCellReuseIdentifier:@"SetHeaderCell"];
    [tableView registerClass:[SettingCell class] forCellReuseIdentifier:@"SettingCell"];
    [self.view addSubview:tableView];
    self.setTabelView = tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        SetHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SetHeaderCell"];
        return cell;
    }
    SettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SettingCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.nameLabel.text = indexPath.row == 1 ? @"更改密码" : @"退出登录";
    cell.iconImage.image = indexPath.row == 1 ? [UIImage imageNamed:@"lock"] : [UIImage imageNamed:@"logout"];

    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return indexPath.row == 0 ? 100 : 60;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 1) {
        if (!USERDEF_GET_VALUE(@"account")){
            [MBProgressHUD showMessag:@"请先登录" toView:self.view];
            return;
        }
        [self changePsd];
    }
    
    if(indexPath.row == 2){
        
        
        CGFloat y = (SCREEN_H-150)/2;
        CGFloat x = 60;
        
        
        _maskView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
        _maskView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
        [self.view.window addSubview:_maskView];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
        [_maskView addGestureRecognizer:tap];
        
        WMCAlertView *psw = [[WMCAlertView alloc]initWithFrame:CGRectMake(x, y, SCREEN_W-x*2, 150)];
        psw.backgroundColor = [UIColor whiteColor];
        [_maskView addSubview:psw];
        
        
        psw.logOutClick = ^(NSString *title){
            
            [_maskView removeFromSuperview];

            if ([title isEqualToString:@"确定"]) {
                
                LoginViewController *login = [[LoginViewController alloc]init];
//                [self.navigationController pushViewController:login animated:NO];
                [self presentViewController:login animated:NO completion:^{
                    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"username"];
                    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"account"];
                    [tableView removeFromSuperview];
//                    tableView = nil;
//                    [self createTable];
                }];
            }
        };
    }
    
    
}

-(void)tapClick{
    [_maskView removeFromSuperview];

}

-(void)changePsd{
    self.setTabelView.alpha = .5;
    if (!self.changePsdView) {
        _changePsdView = [[WMChangePsdView alloc]initWithFrame:CGRectMake(50, SCREEN_H/2-190, SCREEN_W-100, 300)];
        [self.view addSubview:_changePsdView];
        __block typeof(self) blockS = self;
        _changePsdView.changePsdCallBack = ^(NSInteger i,NSMutableArray *textarr) {
            blockS.changePsdView.hidden = YES;
            blockS.setTabelView.alpha = 1;
            
            if (i == 1000) { //修改
                [blockS requestChangeWithArray:textarr];
            }else{   //取消
               
            }
        };
    }else{
        _changePsdView.hidden = NO;
    }
}


-(void)requestChangeWithArray:(NSMutableArray *)arr{
    NSString *oldPsd = @"";
    NSString *changePsd= @"";
    NSString *ageinPsd= @"";
    for (UITextField *textF in arr) {
        NSInteger tag = textF.tag;
        if (tag == 0) {
            oldPsd = textF.text;
        }else if (tag == 10){
            changePsd = textF.text;
        }else{
            ageinPsd = textF.text;
        }
    }
    if (![changePsd isEqual:ageinPsd]) {
        [MBProgressHUD showMessag:@"密码不一致" toView:self.view];
        return;
    }
    NSDictionary *param = @{@"user.usernumber":USERDEF_GET_VALUE(@"account"),
                                @"user.oldpassword":oldPsd,
                                @"user.password":changePsd
                                };
    [[WMManage shareInstance]postRequestWithUrl:[BASE_HTTP stringByAppendingString:@"updatePwd"] andParam:param success:^(id responseObj) {
        if ([responseObj[@"result"] isEqual:@"success"]) {
            [MBProgressHUD showMessag:@"修改成功" toView:self.view];
        }else{
            [MBProgressHUD showMessag:@"修改失败" toView:self.view];
        }
    } fail:^(id error) {
        [MBProgressHUD showMessag:@"请求失败" toView:self.view];
    }];
}

@end
